package cn.aylog.service.impl;

import cn.aylog.dao.IMoneyDao;
import cn.aylog.pojo.Money;
import cn.aylog.service.IMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MoneyServiceImpl implements IMoneyService {

    @Autowired
    private IMoneyDao dao;

    public Money getById(Integer id) {
        return dao.getById(id);
    }

    public void transMoney(Money from, Money to, Integer money) {
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        dao.update(from);
        dao.update(to);
    }

    @Transactional
    public void transMoneyE(Money from, Money to, Integer money) {
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        dao.update(from);
        int i=1/0;
        dao.update(to);
    }

    public void reset() {
        Money m1 = getById(1);
        Money m2 = getById(2);
        m1.setMoney(1000);
        m2.setMoney(1000);
        dao.update(m1);
        dao.update(m2);
    }
}
